package cn.longg.controller;

import cn.longg.entity.Dept;
import cn.longg.entity.Meeting;
import cn.longg.entity.Result;
import cn.longg.entity.User;
import cn.longg.service.IDeptService;
import cn.longg.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.RequestToViewNameTranslator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("main/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IDeptService deptService;

    @RequestMapping("toUser")
    public ModelAndView toUser(){
        return new ModelAndView("user");
    }

    @RequestMapping("list")
    public Result findAll(){
        List<User> users = userService.selectAll();
        return new Result(true,"success",users);
    }

    @RequestMapping("updatePersonal")
    public Result updatePersonal(@RequestBody User user, HttpServletRequest request){
        Dept dept = new Dept();
        dept.setId(user.getDeptId());
        dept = deptService.selectOne(dept);
        user.setDeptName(dept.getName());
        int i = userService.updateByPrimaryKey(user);
        if(0 < i){
            request.getSession().setAttribute("loginUser",user);
            return new Result(true,"更新成功",null);
        }
        return new Result(true,"更新失败",null);
    }

    @RequestMapping("selectByPage/{page}/{pageSize}")
    public Result selectByPage(@PathVariable int page, @PathVariable int pageSize, @RequestBody User user,HttpServletRequest request){
        PageInfo pageInfo = userService.selectByPage(user.getRealName(),page,pageSize);
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        Map<String,Object> map = new HashMap<>();
        map.put("loginUser",loginUser);
        map.put("pageInfo",pageInfo);
        return new Result(true,"查询成功",map);
    }

    @RequestMapping("toDetail")
    public ModelAndView toDetail(){
        return new ModelAndView("user_detail");
    }

    @RequestMapping("lookIncr")
    public Result lookIncr(@RequestBody User user){
        userService.lookIncr(user);
        user.setLook(null);
        user = userService.selectOne(user);
        user.setFansNum(userService.getFansNum(user.getId()));
        return new Result(true,"查看数+1",user);
    }

    @RequestMapping("focus")
    public Result focus(@RequestBody User user,HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if(loginUser.getId() == user.getId()){
            return new Result(false,"不能对自己操作！",null);
        }
        for (Integer focusUser : loginUser.getFocusUsers()) {
            if(user.getId() == focusUser){
                return new Result(false,"你已经关注TA了~",null);
            }
        }
        boolean flag = userService.focus(user,loginUser);
        if(flag){
            loginUser.getFocusUsers().add(user.getId());
            return new Result(true,"关注成功！",null);
        }
        return new Result(flag,"关注失败",null);
    }

    @RequestMapping("unfocus")
    public Result unfocus(@RequestBody User user,HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if(loginUser.getId() == user.getId()){
            return new Result(false,"不能对自己操作！",null);
        }
        boolean flag = true;
        for (Integer focusUser : loginUser.getFocusUsers()) {
            if(user.getId() == focusUser){
                flag = false;
                break;
            }
        }
        if(flag){
            return new Result(false,"你还没有关注他~",null);
        }
        flag = userService.unfocus(user,loginUser);
        if(flag){
            loginUser.getFocusUsers().remove(user.getId());
            return new Result(true,"取关成功！",null);
        }
        return new Result(flag,"取关失败",null);
    }

    @RequestMapping("toMyUser")
    public ModelAndView toMyUser(){
        return new ModelAndView("my_user");
    }

    @RequestMapping("selectFocusUser/{page}/{pageSize}")
    public Result selectFocusUser(@PathVariable int page,@PathVariable int pageSize,HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        PageInfo pageInfo = userService.selectFocusUserByPage(page,pageSize,loginUser);
        if(0 == pageInfo.getList().size()){
            pageInfo = userService.selectFocusUserByPage(page - 1,pageSize,loginUser);
        }
        return new Result(true,"查询成功",pageInfo);
    }

    @RequestMapping("getUserByDeptId")
    public Result getUserByDeptId(@RequestBody Meeting meeting){
        List<User> userList = userService.getUserByDeptId(meeting.getDeptId());
        return new Result(true,"",userList);
    }

    @RequestMapping("upload")
    public Result upload(MultipartFile file, HttpServletRequest request) throws IOException {
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        String picUrl = userService.upload(file,loginUser);
        loginUser.setPic(picUrl);
        request.getSession().setAttribute("loginUser",loginUser);
        return new Result(true,"",picUrl);
    }

}
